import jmespath

from config_params.base_util import baseUtil


class Samplers:

    def __init__(self, data):
        self.data = data

    def jme_path(self, exp):
        return jmespath.search(exp, self.data)

    def HttpRequest(self, class_name):
        http_sampler_dict = {}
        http_sampler_dict["name"] = self.jme_path("name") if self.jme_path("name") is not None else "HTTP Request"
        http_sampler_dict["host"] = self.jme_path("host") if self.jme_path("host") is not None else ""
        http_sampler_dict["path"] = self.jme_path("path") if self.jme_path("path") is not None else "/"
        Method = baseUtil.get_instance_attr("Method")
        if self.jme_path("method").lower() == "get":
            http_sampler_dict["method"] = Method.GET
        elif self.jme_path("method").lower() == "post":
            http_sampler_dict["method"] = Method.POST
        elif self.jme_path("method").lower() == "put":
            http_sampler_dict["method"] = Method.PUT
        elif self.jme_path("method").lower() == "delete":
            http_sampler_dict["method"] = Method.DELETE
        elif self.jme_path("method").lower() == "head":
            http_sampler_dict["method"] = Method.HEAD
        else:
            http_sampler_dict["method"] = Method.GET
        Protocol = baseUtil.get_instance_attr("Protocol")
        if self.jme_path("protocol") is not  None:
            if self.jme_path("protocol").lower() == "http":
                http_sampler_dict["protocol"] = Protocol.HTTP
            elif self.jme_path("protocol").lower() == "https":
                http_sampler_dict["protocol"] = Protocol.HTTPS
            elif self.jme_path("protocol").lower() == "ssh":
                http_sampler_dict["protocol"] = Protocol.SSH
            elif self.jme_path("protocol").lower() == "ftp":
                http_sampler_dict["protocol"] = Protocol.FTP
        else:
            http_sampler_dict["protocol"] = Protocol.HTTP
        # http_sampler_dict["protocol"] = self.jme_path("protocol")
        http_sampler_dict["port"] = self.jme_path("port") if self.jme_path("port") is not None else None
        http_sampler_dict["content_encoding"] = self.jme_path("content_encoding") if self.jme_path("content_encoding") is not None else ""
        http_sampler_dict["auto_redirect"] = self.jme_path("auto_redirect") if self.jme_path("auto_redirect") is not None else False
        http_sampler_dict["keep_alive"] = self.jme_path("keep_alive") if self.jme_path("keep_alive") is not None else  True
        http_sampler_dict["do_multipart_post"] = self.jme_path("do_multipart_post") if self.jme_path("do_multipart_post") is not None else False
        http_sampler_dict["browser_comp_headers"] = self.jme_path("browser_comp_headers") if self.jme_path("browser_comp_headers") is not None else False
        Implement = baseUtil.get_instance_attr("Implement")
        if self.jme_path("implementation") is not  None:
            if self.jme_path("implementation") == '':
                http_sampler_dict["implementation"] = Implement.NONE
            elif self.jme_path("implementation").lower() == 'Java':
                http_sampler_dict["implementation"] = Implement.JAVA
            elif self.jme_path("implementation").lower() == 'HttpClient4':
                http_sampler_dict["implementation"] = Implement.HTTP4CLIENT
        else:
            http_sampler_dict["implementation"] = Implement.NONE
        # http_sampler_dict["implementation"] = self.jme_path("implementation")
        http_sampler_dict["connect_timeout"] = self.jme_path("connect_timeout") if self.jme_path("connect_timeout") is not None else None
        http_sampler_dict["response_timeout"] = self.jme_path("response_timeout") if self.jme_path("response_timeout") is not  None else None
        http_sampler_dict["retrieve_all_emb_resources"] = self.jme_path("retrieve_all_emb_resources") if self.jme_path("retrieve_all_emb_resources") is not None else False
        http_sampler_dict["parallel_downloads"] = self.jme_path("parallel_downloads") if self.jme_path("parallel_downloads") is not None else False
        http_sampler_dict["parallel_downloads_no"] = self.jme_path("parallel_downloads_no") if self.jme_path("parallel_downloads_no") is not None else None
        http_sampler_dict["url_must_match"] = self.jme_path("url_must_match") if self.jme_path("url_must_match") is not None else ""
        Source = baseUtil.get_instance_attr("Source")
        if self.jme_path("source_type") is not None:
            if self.jme_path("source_type") == 'hostname':
                http_sampler_dict["source_type"] = Source.HOSTNAME
            elif self.jme_path("source_type") == 'device':
                http_sampler_dict["source_type"] = Source.DEVICE
            elif self.jme_path("source_type") == 'ipv4':
                http_sampler_dict["source_type"] = Source.IPV4
            elif self.jme_path("source_type") == 'ipv6':
                http_sampler_dict["source_type"] = Source.IPV6
        else:
            http_sampler_dict["source_type"] = Source.HOSTNAME
        # http_sampler_dict["source_type"] = self.jme_path("source_type") if  self.jme_path("source_type")  is not None else ""
        http_sampler_dict["source_address"] = self.jme_path("source_address") if self.jme_path("source_address") is not  None else ""
        http_sampler_dict["proxy_scheme"] = self.jme_path("proxy_scheme") if self.jme_path("proxy_scheme") is not None else ""
        http_sampler_dict["proxy_host"] = self.jme_path("proxy_host") if self.jme_path("proxy_host") is not None else ""
        http_sampler_dict["proxy_port"] = self.jme_path("proxy_port") if self.jme_path("proxy_port") is not None else None
        http_sampler_dict["proxy_username"] = self.jme_path("proxy_username") if self.jme_path("proxy_username") is not None else ""
        http_sampler_dict["proxy_password"] = self.jme_path("proxy_password") if self.jme_path("proxy_password") is not None else ""
        http_sampler_dict["comments"] = self.jme_path("comments") if self.jme_path("comments") is not None else ""
        http_sampler_dict["is_enabled"] = self.jme_path("is_enabled") if self.jme_path("is_enabled") is not None else True
        http_sampler_obj = baseUtil.create_instance(class_name, http_sampler_dict)
        return http_sampler_obj

    def sampler(self):
        samplers_list = []
        if self.jme_path("type") == "HttpRequest":
            samplers_list.append(self.HttpRequest(self.jme_path("type")))
        return samplers_list
